package pl.edu.pw.elka.dzielins.revanalyzer.instance;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import pl.edu.pw.elka.dzielins.revanalyzer.model.ModelFact;
import pl.edu.pw.elka.dzielins.revanalyzer.model.ModelObject;

public class Instance {
	private Set<InstanceObject> objects;
	
	public Instance() {
		this.objects=new HashSet<InstanceObject>();
	}
	
	// getters & setters

	public Set<InstanceObject> getObjects() {
		return objects;
	}

	public void setObjects(Set<InstanceObject> objects) {
		this.objects = objects;
	}
	
	// artificial getters & setters
	
	public Map<ModelObject,Set<InstanceObject>> getInheritanceMap() {
		Map<ModelObject, Set<InstanceObject>> map = new HashMap<ModelObject, Set<InstanceObject>>();
		Set<InstanceObject> set;
		for (InstanceObject object : objects) {
			if (!map.containsKey(object.getBaseObject()))
				map.put(object.getBaseObject(), new HashSet<InstanceObject>());
			set = map.get(object.getBaseObject());
			set.add(object);
		}
		return map;
	}
	
	public void removeObject(InstanceObject object) {
		// remove relations where object is referenced
		Set<InstanceRelation> relations = new HashSet<InstanceRelation>();
		for (InstanceObject o : objects) {
			if (o == object)
				continue;
			relations.addAll(o.getRelationsWithObject(object));
		}
		InstanceObject o;
		for (InstanceRelation relation : relations) {
			o = relation.getOwner();
			o.removeRelation(relation);
		}
		// remove object
		objects.remove(object);
	}
	
	// other methods
	
	@Override
	public String toString() {
		String s="";
		for(InstanceObject object : objects)
			s+=object.toString()+"\n";
		return s;
	}
}
